package org.springmvc.mybatis.pagination;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页查询分页参数封装
 *
 * @author tangjialin on 2018-08-23.
 */
public class Pageable implements Serializable {
    /** 默认每页数据条数 */
    public static final int DEFAULT_PAGE_SIZE = 10;
    /** 每页数据条数最大值 */
    public static final int MAX_PAGE_SIZE = 1000;
    /** 默认当前页码 */
    public static final int DEFAULT_PAGE_NUMBER = 1;

    /** 每页显示数据条数 */
    private int pageSize = DEFAULT_PAGE_SIZE;
    /** 页码 */
    private int pageNumber = DEFAULT_PAGE_NUMBER;
    /** 查询条件集 */
    private List<QueryCondition> queryConditions;

    /** 记录总数 */
    private long total;

    public Pageable() {
    }

    public Pageable(int pageNumber) {
        setPageNumber(pageNumber);
    }

    public Pageable(int pageNumber, int pageSize) {
        setPageNumber(pageNumber);
        setPageSize(pageSize);
    }

    public int getPageSize() {
        return pageSize;
    }

    public Pageable setPageSize(int pageSize) {
        this.pageSize = pageSize < DEFAULT_PAGE_SIZE || pageSize > MAX_PAGE_SIZE ? DEFAULT_PAGE_SIZE : pageSize;
        return this;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public Pageable setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber < DEFAULT_PAGE_NUMBER ? DEFAULT_PAGE_NUMBER : pageNumber;
        return this;
    }

    public List<QueryCondition> getQueryConditions() {
        return queryConditions == null ? (queryConditions = new ArrayList<>()) : queryConditions;
    }

    public Pageable setQueryConditions(List<QueryCondition> queryConditions) {
        this.queryConditions = queryConditions;
        return this;
    }

    public long getTotal() {
        return total;
    }

    public Pageable setTotal(long total) {
        this.total = total;
        return this;
    }

    public Pageable addQueryCondition(QueryCondition queryCondition) {
        getQueryConditions().add(queryCondition);
        return this;
    }
}
